<%@page import="com.tubeonfire.entity.Channel"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="java.util.TreeMap"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	ImagesService imagesService = ImagesServiceFactory.getImagesService(); 			
	Channel obj = (Channel)session.getAttribute("obj");
	String imgUrl = obj.getImageKey();
	if(!imgUrl.contains("http://")){
		try{
			BlobKey blobKey = new BlobKey(imgUrl);
			imgUrl = imagesService.getServingUrl(blobKey);
		}catch(Exception e){
			imgUrl = "/images/tube.png";
		}														
	}	
%>
<jsp:include page="/admin/elements/header.jsp">
	<jsp:param value="Channel Form" name="title"/>
</jsp:include>
<script type="text/javascript" src="/admin/js/mine/admin.js"></script>
<script type="text/javascript">
	jQuery(document).ready(function() { 		
		jQuery("#btnSubmit").click(function(){			
			onSubmitForm();
			return false;
		}); 
		jQuery('#btnDelete').click(function(){			
			var title = jQuery(this).attr("rel");
			var c = confirm('Are you sure want to delete \"' + title + '\" channel ?');
			if(c) {
				showAjaxEffect();
				var id = jQuery(this).attr("href");				
				jQuery.get("/admin/channel/process", {id: id, action: "delete"}, function(data) {				
					hideAjaxEffect();
					scrollToTop();
					if(data==''){			
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
						jQuery('#messageDiv > p').html('Action success ! Channel has been deleted. Redirecting...');
						setTimeout(function() {							
							window.location.href = '/admin/channel/list';
						}, 2000);	
					}else{
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
						jQuery('#messageDiv > p').html(data);
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}	
				});					
			}
			return false;
		});
	});    	
	function onSubmitForm(){         	
    	jQuery.get("/admin/upload/get-url?returnUrl=" + "/admin/channel/edit", function(data){        		
    		jQuery("#channelForm").attr("action", data);      		
    		jQuery("#channelForm").submit();    		
    	});        	
    }
</script>

	<body class="loggedin">
		<jsp:include page="/admin/elements/top.jsp"/>
	    <!-- START OF MAIN CONTENT -->
	    <div class="mainwrapper">
	     	<div class="mainwrapperinner">         	        
				<jsp:include page="/admin/elements/left.jsp">
					<jsp:param value="channel" name="current_page"/>
				</jsp:include>					              	                   
	        	<div class="maincontent noright">
	        		<div class="maincontentinner">	            	
		                <ul class="maintabmenu">
		                	<li><a href="/admin/channel/list">Manage Channel</a></li>
		                	<li class="current"><a href="/admin/channel/edit?id=<%=obj.getId()%>">Edit Channel</a></li>
		                </ul><!--maintabmenu-->	                
	                	<div class="content">	
	                	<jsp:include page="/admin/elements/message.jsp"/>
	                	<div id="messageDiv" class="notification hidden">						   
						    <p></p>
						</div>	   	                    
	                    	<div class="contenttitle">
	                    		<h2 class="image"><span>Fill Channel Information</span></h2>
	                    	</div>
	                    	<br />
		                    <form class="stdform" enctype="multipart/form-data"  action="" method="post" id="channelForm">
		                   	<input type="hidden" name="id" value="<%=obj.getId()%>">                 	
		                        <p>
		                        	<label>Title</label>
		                            <span class="field">
		                            	<input type="text" name="title" class="smallinput" value="<%=obj.getTitle()%>"/>		                            	
		                            </span>		                            		                           
		                        </p>                                                                      
		                        <p>
		                        	<label>Description</label>
		                            <span class="field">
		                            	<textarea cols="80" rows="5" class="longinput" name="description"><%=obj.getDescription()%></textarea>		                            			                           
									</span>
									<small class="desc">Small description of this channel.</small>
		                        </p>    		                                         
		                        <p>
		                        	<label>Cover Image</label>
		                            <span class="field">
		                            	<input type="file" name="image" class="smallinput">		                            	
										<br/><br/>
										<img src="<%=imgUrl %>" alt="" style="max-width: 250px; max-height: 150px"/>	   
		                            </span> 
		                        </p>                                                                                                                                          
                        		<br clear="all" />                  
		                        <p class="stdformbutton">
		                        	<button class="submit radius2" id="btnSubmit">Submit</button>
		                            <input type="reset" class="reset radius2" value="Reset" />		                            
		                           	<span style="padding-left: 30px">		                            	
		                         		<a href="/admin/tube/list?channelId=<%=obj.getId() %>" id="btnBanner" class="btn btn_tubes" title="View tubes of this channel"><span>View Tubes</span></a>&nbsp;&nbsp;
		                         		<a href="<%=obj.getId() %>" id="btnDelete" class="btn btn_trash" title="Delete this channel" rel="<%=obj.getTitle()%>"><span>Delete</span></a>
		                            </span>	
		                        </p>                                                
                    		</form>             
	                    <br /><br />	                    
	                </div><!--content-->	                
	            </div><!--maincontentinner-->
				<jsp:include page="/admin/elements/bottom.jsp"></jsp:include>                    	           	
	        </div><!--maincontent-->	                        
	     	</div><!--mainwrapperinner-->
	    </div><!--mainwrapper-->
		<!-- END OF MAIN CONTENT -->    
	</body>
</html>
